package com.tanhua.server.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.server.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@Service
public class UserService {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Autowired
    private RestTemplate restTemplate;

    @Value("${tanhua.sso.url}")
    private String url;

    /**
     * 使用RestTemplate调用sso的接口，获取到User对象
     *
     * @param token
     * @return
     */
    public User queryUserByToken(String token){
        String data = restTemplate.getForObject(url+"/user/{token}",String.class,token);
        if (data == null){
            return null;
        }
        try {
            return MAPPER.readValue(data, User.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
